﻿using System;
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ShakeIt_.ServiceReference2;

namespace ShakeIt_
{
    public class Data
    {
        public static string username = "";
        public static int userId = -1;
        public static int userFocusedId = -1;
        public static string userFocusedName = "";
        public static string userFocusedPhone = "";
        public static string searchInput = "";
        public static _User currentUser = new _User();
        public static List<_User> userList = new List<_User>();
        public static List<_Activity> result = new List<_Activity>();
        public static List<_Activity> resList_selected = new List<_Activity>();
        public static _Activity res_selected = new _Activity();
        public static int res_Count = 0;
        public static List<_UserComment> myComment = new List<_UserComment>();
        public static List<_UserComment> commentResearch = new List<_UserComment>();
        public static double myScore = -1;
        public static double scoreResearch = -1;
        public static List<_User> fansList = new List<_User>();
        public static List<_User> focusList = new List<_User>();
        public static bool focus = false;
        public static List<_Chat> messageList = new List<_Chat>();
        public static string DebugString = "";
        public static Service1Client proxy = new Service1Client();
        public static System.Device.Location.GeoCoordinate currentLocation = new System.Device.Location.GeoCoordinate(31.200833, /*121.429726*/121.43);
        public Data()
        {
            //加入proxy的回调函数
            proxy.FindActivityByLocationCompleted += new EventHandler<FindActivityByLocationCompletedEventArgs>(proxy_FindActivityByLocationCompleted);
            proxy.FindActivityByLocationAndKeywordCompleted += new EventHandler<FindActivityByLocationAndKeywordCompletedEventArgs>(proxy_FindActivityByLocationAndKeywordCompleted);
            proxy.FindUserCompleted += new EventHandler<FindUserCompletedEventArgs>(proxy_FindUserCompleted);
            proxy.GetAllCommentCompleted += new EventHandler<GetAllCommentCompletedEventArgs>(proxy_GetAllCommentCompleted);
            proxy.IsFocusCompleted += new EventHandler<IsFocusCompletedEventArgs>(proxy_IsFocusCompleted);
            proxy.FindChatByReceiveIdCompleted += new EventHandler<FindChatByReceiveIdCompletedEventArgs>(proxy_FindChatByReceiveIdCompleted);
            proxy.InsertChatCompleted += new EventHandler<InsertChatCompletedEventArgs>(proxy_InsertChatCompleted);
            proxy.InsertCommentCompleted += new EventHandler<InsertCommentCompletedEventArgs>(proxy_InsertCommentCompleted);
            proxy.GetFansCompleted += new EventHandler<GetFansCompletedEventArgs>(proxy_GetFansCompleted);
            proxy.GetFocusCompleted += new EventHandler<GetFocusCompletedEventArgs>(proxy_GetFocusCompleted);
        }
        public static void FindActivity()
        {
            decimal longitude = (decimal)currentLocation.Longitude;
            decimal latitude = (decimal)currentLocation.Latitude;
            if (Data.searchInput.Equals(""))
            {
                proxy.FindActivityByLocationAsync(longitude, latitude);
            }
            else
            {
                proxy.FindActivityByLocationAndKeywordAsync(longitude, latitude, Data.searchInput);
            }
        }
        public static void calculateScore(string username)
        {
            double sum = 0;
            double count = 0;
            double res = 0;
            foreach (_UserComment uc in commentResearch)
            {
                sum += uc.score;
                count++;
            }
            if (count != 0)
            {
                res = sum / count;
            }
            if (username == Data.username)
            {
                myScore = res;
            }
            scoreResearch = res;
        }
        public static _UserComment GetRecentComment()
        {
            if (commentResearch.Count == 0)
            {
                return null;
            }
            return commentResearch[commentResearch.Count - 1];
        }
        private void proxy_FindActivityByLocationCompleted(object sender, FindActivityByLocationCompletedEventArgs e)
        {
            result = e.Result.ToList();
            resList_selected = result;
        }
        private void proxy_FindActivityByLocationAndKeywordCompleted(object sender, FindActivityByLocationAndKeywordCompletedEventArgs e)
        {
            result = e.Result.ToList();
            resList_selected = result;
        }
        private void proxy_FindUserCompleted(object sender, FindUserCompletedEventArgs e)
        {
            _User u = new _User();
            userList = e.Result.ToList();
            u = userList[0];
            if (u.id == userId)
            {
                currentUser = u;
            }
        }
        private void proxy_GetAllCommentCompleted(object sender, GetAllCommentCompletedEventArgs e)
        {
            commentResearch = e.Result.ToList();
            if (commentResearch.Count != 0)
            {
                if (commentResearch[0].user == username)
                {
                    myComment = commentResearch;
                }
            }
        }
        private void proxy_GetFansCompleted(object sender, GetFansCompletedEventArgs e)
        {
            fansList = e.Result.ToList();
        }
        private void proxy_GetFocusCompleted(object sender, GetFocusCompletedEventArgs e)
        {
            focusList = e.Result.ToList();
        }
        private void proxy_IsFocusCompleted(object sender, IsFocusCompletedEventArgs e)
        {
            focus = e.Result;
        }
        private void proxy_FindChatByReceiveIdCompleted(object sender, FindChatByReceiveIdCompletedEventArgs e)
        {
            messageList = e.Result.ToList();
        }
        private void proxy_InsertCommentCompleted(object sender, InsertCommentCompletedEventArgs e)
        {
            DebugString = e.Result;
        }
        private void proxy_InsertChatCompleted(object sender, InsertChatCompletedEventArgs e)
        {
            DebugString = e.Result;
        }
    }
}
